//
// Created by Administrator on 2024/3/11.
//
#include<bits/stdc++.h>

using namespace std;
using ll = long long;
#define endl '\n'

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    deque<int> Q;
    int q;
    cin >> q;
    while (q--) {
        int op;
        cin >> op;
        if (op == 0) {
            int d, x;
            cin >> d >> x;
            if (d == 1)Q.push_back(x);
            else if (d == 0)Q.push_front(x);
        } else if (op == 1) {
            int x;
            cin >> x;
            cout << Q[x] << '\n';
        } else {
            int d;
            cin >> d;
            if (d == 1)Q.pop_back();
            else Q.pop_front();
        }
    }

    return 0;
}

